package cn.edu.ctbu.eol.teacher.service.impl;

import cn.edu.ctbu.eol.teacher.pojo.Teacher;

import cn.edu.ctbu.eol.teacher.mapper.TeacherMapper;
import cn.edu.ctbu.eol.teacher.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherServiceimpl implements TeacherService{

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public Map<String, Object> getTeachersByPage(String name,String employeeNumber,Integer page, Integer limit) {
        Map<String, Object> result = new HashMap<>();

        Integer offset = (page - 1) * limit;

        List<Teacher> teachers = teacherMapper.selectByPage(name, employeeNumber,offset, limit);

        Integer count = teacherMapper.selectCount(name,employeeNumber);

        result.put("code", 0);
        result.put("msg", "");
        result.put("count", count);
        result.put("data", teachers);

        return result;
    }



    @Override
    public boolean addTeacher(Teacher teacher) {
        String password = DigestUtils.md5DigestAsHex(teacher.getPassword().getBytes(StandardCharsets.UTF_8));
        teacher.setPassword(password);
        return teacherMapper.insert(teacher) > 0;
    }

    @Override
    public boolean updateTeacher(Teacher teacher) {
        String password = DigestUtils.md5DigestAsHex(teacher.getPassword().getBytes(StandardCharsets.UTF_8));
        teacher.setPassword(password);
        return teacherMapper.update(teacher) > 0;
    }

    @Override
    public boolean deleteTeacher(Integer id) {
        return teacherMapper.delete(id) > 0;
    }

    @Override
    public Teacher getByEmployeeNumber(String employeeNumber) {
        Teacher teacher = teacherMapper.getByEmployeeNumber(employeeNumber);
        return teacher;
    }
}